#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
Simplified BSD License (BSD)
Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

using System;
using System.Drawing;

namespace Thn.Drawing
{
    public class ColorConverter
    {
        #region HSL to Argb
        /// <summary>
        /// Converts from an HSL color to Argb (system) color
        /// </summary>
        public static Color HSLmtomArgb(HslColor hsl)
        {
            byte red;
            byte green;
            byte blue;
            float Saturation = hsl.Saturation;
            float Luminance = hsl.Luminance;
            float Hue = hsl.Hue;

            if (Saturation == 0.0)
            {
                red = (byte)(Luminance * 255.0F);
                green = red;
                blue = red;
            }
            else
            {
                float rm1;
                float rm2;

                if (Luminance <= 0.5f)
                {
                    rm2 = Luminance + Luminance * Saturation;
                }
                else
                {
                    rm2 = Luminance + Saturation - Luminance * Saturation;
                }
                rm1 = 2.0f * hsl.Luminance - rm2;
                red = ToRGB1(rm1, rm2, Hue + 120.0f);
                green = ToRGB1(rm1, rm2, Hue);
                blue = ToRGB1(rm1, rm2, Hue - 120.0f);
            }

            return Color.FromArgb(hsl.Alpha, red, green, blue);
        }

        static byte ToRGB1(float rm1, float rm2, float rh)
        {
            if (rh > 360.0f)
            {
                rh -= 360.0f;
            }
            else if (rh < 0.0f)
            {
                rh += 360.0f;
            }

            if (rh < 60.0f)
            {
                rm1 = rm1 + (rm2 - rm1) * rh / 60.0f;
            }
            else if (rh < 180.0f)
            {
                rm1 = rm2;
            }
            else if (rh < 240.0f)
            {
                rm1 = rm1 + (rm2 - rm1) * (240.0f - rh) / 60.0f;
            }

            return (byte)(rm1 * 255);
        }
        #endregion

        #region Argb to HSL
        /// <summary>
        /// Converts from an Argb (system) color to HSL
        /// </summary>
        public static HslColor ArgbmtomHSL(Color Argb)
        {
            float luminance = 0f;
            float saturation = 0f;
            float hue = 0f;
            byte red = Argb.R;
            byte green = Argb.G;
            byte blue = Argb.B;

            byte minval = Math.Min(red, Math.Min(green, blue));
            byte maxval = Math.Max(red, Math.Max(green, blue));

            float mdiff = (float)(maxval - minval);
            float msum = (float)(maxval + minval);

            luminance = msum / 510.0f;

            if (maxval == minval)
            {
                saturation = 0.0f;
                hue = 0.0f;
            }
            else
            {
                float rnorm = (maxval - red) / mdiff;
                float gnorm = (maxval - green) / mdiff;
                float bnorm = (maxval - blue) / mdiff;

                saturation = (luminance <= 0.5f) ? (mdiff / msum) : (mdiff / (510.0f - msum));

                if (red == maxval)
                {
                    hue = 60.0f * (6.0f + bnorm - gnorm);
                }
                if (green == maxval)
                {
                    hue = 60.0f * (2.0f + rnorm - bnorm);
                }
                if (blue == maxval)
                {
                    hue = 60.0f * (4.0f + gnorm - rnorm);
                }
                if (hue > 360.0f)
                {
                    hue = hue - 360.0f;
                }
            }

            HslColor result = new HslColor(hue, saturation, luminance);
            result.Alpha = Argb.A;

            return result;
        }
        #endregion

        #region CYMK to Argb
        /// <summary>
        /// Converts from a CYMK color to Argb (system) color
        /// </summary>
        public static Color CYMKmtomArgb(CymkColor cymk)
        {
            byte red, green, blue;

            red = Round(255 - (255 * cymk.Cyan));
            green = Round(255 - (255 * cymk.Magenta));
            blue = Round(255 - (255 * cymk.Yellow));

            return Color.FromArgb(cymk.Alpha, red, green, blue);
        }

        /// <summary>
        /// Custom rounding function.
        /// </summary>
        /// <param name="val">Value to round</param>
        /// <returns>Rounded value</returns>
        private static byte Round(float val)
        {
            byte retmval = (byte)val;

            byte temp = (byte)(val * 100);

            if ((temp % 100) >= 50)
                retmval += 1;

            return retmval;
        }
        #endregion

        #region Argb to CYMK
        /// <summary>
        /// Converts from a Argb(system) color to CYMK
        /// </summary>
        public static CymkColor ArgbmtomCYMK(Color c)
        {
            CymkColor mcmyk = new CymkColor();
            mcmyk.Alpha = c.A;
            float low = 1f;

            mcmyk.Cyan = (float)(255 - c.R) / 255;
            if (low > mcmyk.Cyan)
                low = mcmyk.Cyan;

            mcmyk.Magenta = (float)(255 - c.G) / 255;
            if (low > mcmyk.Magenta)
                low = mcmyk.Magenta;

            mcmyk.Yellow = (float)(255 - c.B) / 255;
            if (low > mcmyk.Yellow)
                low = mcmyk.Yellow;

            if (low > 0.0)
            {
                mcmyk.Black = low;
            }

            return mcmyk;
        }
        #endregion
    }
}
